package jp.ac.aiit.yc.softarch.hbcp.service.common;

import java.util.ArrayList;
import java.util.List;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

import jp.ac.aiit.yc.softarch.hbcp.model.common.MeetingRoom;

public class MeetingRoomService {

	private static List<MeetingRoom> meetingRooms = new ArrayList<MeetingRoom>();

	public void saveMeetingRoom(MeetingRoom meetingRoom) {
		System.out.println("saveMeetingRoom");
		for (int i = 0;i < meetingRooms.size();i++) {
			if (meetingRooms.get(i).getUid().equals(meetingRoom.getUid())) {
				meetingRooms.set(i, meetingRoom);
				return ;
			}
		}
		try {
			meetingRooms.add(meetingRoom);
			MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
			String clientID = UUIDUtils.createUUID();

			AsyncMessage msg = new AsyncMessage();
			msg.setDestination("hbcp");
			msg.setClientId(clientID);
			msg.setMessageId(UUIDUtils.createUUID());
			msg.setTimestamp(System.currentTimeMillis());
			msg.setBody(meetingRoom);
			msg.setHeader("operation", "updateMeetingRoom");
			msgBroker.routeMessageToService(msg, null);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public List<MeetingRoom> getMeetingRooms() {
		System.out.println("getMeetingRooms");
		return meetingRooms;
	}
}
